import { fileURLToPath, URL } from "node:url";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import path from 'path';
import dts from 'vite-plugin-dts';

// 发布版配置：绕开模块联邦，直接集成所有模块
export default defineConfig({
  plugins: [
    vue(),
    // 注意：移除了federation插件
    dts({
      include: ['src/components/**/*.ts', 'src/components/**/*.d.ts', 'src/components/**/*.vue'],
      outDir: 'dist',
      staticImport: true,
      insertTypesEntry: true,
      cleanVueFileName: true,
      copyDtsFiles: true
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
      symbolId: "i-[dir]-[name]"
    }),
    AutoImport({
      resolvers: [
        IconsResolver({ prefix: "Icon" }),
        ElementPlusResolver()
      ],
    }),
    Components({
      resolvers: [
        IconsResolver({ enabledCollections: ["ep"] }),
        ElementPlusResolver()
      ],
    }),
    Icons({
      autoInstall: true,
      compiler: "vue3"
    })
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    port: 3000,
    fs: {
      allow: ['D:\\modules', 'C:\\', '/']
    }
  },
  optimizeDeps: {
    include: [
      'vue', 'vue-router', 'vue-i18n', 'vue-demi', 'vue-virtual-scroller', 'vuedraggable',
      'pinia',
      'element-plus', '@element-plus/icons-vue',
      'lodash', 'moment', 'js-cookie', 'axios', 'better-mock', 'echarts', 'split.js', 'uuid', '@microsoft/signalr',
      '@xc-components/xc-form', '@xc-components/xc-table',
      '@iconify-json/ep'
    ],
    force: true,
  },
  build: {
    emptyOutDir: true,
    outDir: path.resolve(__dirname, './dist'),
    target: ['es2022', 'edge89', 'firefox89', 'chrome89', 'safari15'],
    rollupOptions: {
      external: [],
      output: {
        globals: {},
        manualChunks: undefined,
        exports: 'named',
        interop: 'auto',
        preserveModules: false,
      }
    },
    minify: true,
    sourcemap: false,
    copyPublicDir: true,
  },
});